<?php
/**
 * This file contains a class to create a button
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 S.Ruttloff, Computer Hard- & Software
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: yui.button.class.php 100 2010-07-11 16:29:44Z sruttloff@googlemail.com $
 * @link      PHPMY
 */

/**
 * init smarty config and return smarty clone object
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 S.Ruttloff, Computer Hard- & Software
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      PHPMY
 */
class yuiButton
{
    /**
     * instance holder
     * @var array
     */
    static private $_instance = array();
    /**
     * configuration of button instance
     *
     * @var array
     */
    protected $a_CONFIGURATION = array();
    /**
     * def. constructor
     *
     * @param string $buttonInstance instance name of button
     *
     * @return void
     */
    private function __construct($buttonInstance = 'default')
    {
        // set id of button element
        $this->a_CONFIGURATION['id'] = $buttonInstance;
    }
    /**
     * create or get a instance of object
     *
     * @param string $buttonInstance instance name of button
     *
     * @return yuiButton onstance of it self
     */
    static public function getInstance($buttonInstance = 'default')
    {
        if (!isset(self::$_instance[$buttonInstance])) self::$_instance[$buttonInstance] = new self($buttonInstance);
        return self::$_instance[$buttonInstance];
    }
    /**
     * render push button code
     *
     * @param string $s_container element id to render button in
     * @param array  $a_Config    configuration of button
     *
     * @return string js code to render a push button
     */
    public function renderPushButton($s_container = null, $a_Config = array())
    {
        if (!$s_container) throw new Exception('button container not set');
        $this->setConfiguration(array('container' => $s_container));
        if ($a_Config && is_array($a_Config)) $this->setConfiguration($a_Config);
        $s = 'var ' . $this->a_CONFIGURATION['id'] . ' = new YAHOO.widget.Button({';
        $s_on = $s_config = null;
        foreach ($this->a_CONFIGURATION as $k=>$v)
        {
            switch ($k)
            {
                case 'onclick':
                    $s_on .= $this->a_CONFIGURATION['id'] . '.on("click", function(){' .  $v . '});';
                    break;
                case 'disabled':
                    $s_config .= ($s_config ? ', ' : '' );
                    $s_config .= $k . ': ' . ($v ? 'true' : 'false') . '';
                    break;
                default:
                    $s_config .= ($s_config ? ', ' : '' );
                    $s_config .= $k . ': "' . $v . '"';
                    break;
            }
        }
        $s .= $s_config . '});' . $s_on;
        return $s;
    }
    /**
     * set a button configuration
     *
     * @param array $a_Config configuration of button
     *
     * @return bool
     */
    public function setConfiguration($a_Config = array())
    {
        if (!is_array($a_Config))
            throw new Exception('button configuration must array type');
        foreach ($a_Config as $k=>$v)
        {
            $this->a_CONFIGURATION[$k] = $v;
        }
        return true;
    }
}